﻿using System;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;

namespace SPDevExplorer
{
    public partial class SiteExplorer
    {
        private DTE2 _dte;
        private EnvDTE.Events _events;

        public DTE2 DTE
        {
            get { return _dte; }
            set
            {
                _dte = value;
                _events = _dte.Events;
            }
        }

        private bool ServicesAllowed
        {
            get
            {
                if (DTE.Debugger.CurrentMode == dbgDebugMode.dbgBreakMode) return false;

                return true;
            }
        }

        /// <summary>
        /// Open a working folder file in Visual Studio or other application
        /// </summary>
        /// <param name="fileNode">the file tree node to open from working folder</param>
        private void OpenWorkingFolderFileInVisualStudioOrApplication(TreeNode fileNode)
        {
            string sPath = FileNodeToWorkingFolderFilePath(fileNode);
            Window w = DTE.ItemOperations.OpenFile(sPath, EnvDTE.Constants.vsViewKindAny);
            if (w == null)
            {
                // opened outside of VS.Net
                return;
            }

            DocumentEvents de = _events.get_DocumentEvents(w.Document);
            _docNodes.Add(sPath, fileNode);
            _docEvents.Add(sPath, de);
            _windows.Add(sPath, w);
            de.DocumentSaved += SiteExplorerDocumentSaved;
            de.DocumentClosing += SiteExplorerDocumentClosing;
        }

        /// <summary>
        /// Event handler for a closing document.
        /// </summary>
        /// <param name="doc">The closing document</param>
        private void SiteExplorerDocumentClosing(Document doc)
        {
            try
            {
                _docEvents.Remove(doc.Path + doc.Name);
                _docNodes.Remove(doc.Path + doc.Name);
                _windows.Remove(doc.Path + doc.Name);
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }

        /// <summary>
        /// Event handler for a saved document. Load document from working folder and save to SharePoint.
        /// </summary>
        /// <param name="doc">The saved document</param>
        private void SiteExplorerDocumentSaved(Document doc)
        {
            try
            {
                LogEvent(2, "Saving file: " + doc.Path + doc.Name);

                TreeNode n = _docNodes[doc.Path + doc.Name];
                FileProperties p = new FileProperties(n);

#if true
                // SvdO: save working folder file to sharepoint in its binary form
                SaveFileFromWorkingFolderToSharePoint(n);
#else
                byte[] arFile = 
                using (StreamReader rdr = File.OpenText(doc.Path + doc.Name))
                {
                    string sFile = rdr.ReadToEnd();
                    DevServices(n).SaveFile(p.Url, sFile);
                }
#endif
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }
    }
}